package com.hdw.med.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hdw.common.constant.CommonConstant;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.MdMedPubfldService;
import com.hdw.med.bean.model.MdMedPubfld;
import com.hdw.med.mapper.MdMedPubfldMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mdMedPubfld服务实现类
 * Created by hozeData on 2021/07/05.
 */
@Service
@Transactional
public class MdMedPubfldServiceImpl implements MdMedPubfldService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MdMedPubfldServiceImpl.class);

    @Autowired
    private MdMedPubfldMapper mdMedPubfldMapper;

    @Override
    public int insert(MdMedPubfld mdMedPubfld) {
        if (StringUtils.isEmpty(mdMedPubfld.getIdPubfld())) {
            mdMedPubfld.setIdPubfld(mdMedPubfld.getCd());
        }
        return mdMedPubfldMapper.insert(mdMedPubfld);
    }

    @Override
    public int insertSelective(MdMedPubfld mdMedPubfld) {
        if (StringUtils.isEmpty(mdMedPubfld.getIdPubfld())) {
            mdMedPubfld.setIdPubfld(mdMedPubfld.getCd());
        }
        return mdMedPubfldMapper.insertSelective(mdMedPubfld);
    }

    @Override
    public int deleteById(String idPubfld) {
        return mdMedPubfldMapper.deleteByPrimaryKey(idPubfld);
    }

    @Override
    public int updateByIdSelective(MdMedPubfld mdMedPubfld) {

        return mdMedPubfldMapper.updateByPrimaryKeySelective(mdMedPubfld);
    }

    @Override
    public int updateById(MdMedPubfld mdMedPubfld) {
        return mdMedPubfldMapper.updateByPrimaryKey(mdMedPubfld);
    }

    @Override
    public MdMedPubfld getById(String idPubfld) {
        return mdMedPubfldMapper.selectByPrimaryKey(idPubfld);
    }

    @Override
    public List<MdMedPubfld> findByModel(MdMedPubfld mdMedPubfld) {
        return mdMedPubfldMapper.findByModel(mdMedPubfld);
    }

    @Override
    public PaginationVO<MdMedPubfld> findPageByModel(MdMedPubfld mdMedPubfld, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        List<MdMedPubfld> quesList = mdMedPubfldMapper.findByModel(mdMedPubfld);
        PageInfo<MdMedPubfld> pageInfo = new PageInfo<>(quesList);
        PaginationVO<MdMedPubfld> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

    @Override
    public PaginationVO<MdMedPubfld> findPageByLike(String filter, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        List<MdMedPubfld> quesList = mdMedPubfldMapper.findByLike(filter);
        PageInfo<MdMedPubfld> pageInfo = new PageInfo<>(quesList);
        PaginationVO<MdMedPubfld> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

    @Override
    public List<MdMedPubfld> findByIdList(List<String> idList) {
        return mdMedPubfldMapper.findByIdList(idList);
    }

    @Override
    public Map<String, MdMedPubfld> findMapDataByIdList(List<String> idList) {
        List<MdMedPubfld> list = mdMedPubfldMapper.findByIdList(idList);
        Map<String, MdMedPubfld> map = new HashMap<>();
        for (MdMedPubfld fld : list) {
            map.put(fld.getIdPubfld(), fld);
        }
        return map;
    }


    @Override
    public List<MdMedPubfld> findByFldPrefix(String idPubfld) {
        return mdMedPubfldMapper.findByFldPrefix(idPubfld);
    }

    @Override
    public String getCdDimGrp(List<String> idList) {
        List<MdMedPubfld> list = this.findByIdList(idList);
        String cdDimgrp = "";
        for (MdMedPubfld fld : list) {
            cdDimgrp += fld.getCdDim() + "_";
        }
        return cdDimgrp.substring(0, cdDimgrp.length() - 1);
    }

    @Override
    public List<String> findDimTable() {
        return mdMedPubfldMapper.findDimTable();
    }

    @Override
    public PaginationVO<MdMedPubfld> findIsDimPubfld(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize == null ? 10 : pageSize);
        List<MdMedPubfld> quesList = mdMedPubfldMapper.findIsDimPubfld();
        PageInfo<MdMedPubfld> pageInfo = new PageInfo<>(quesList);
        PaginationVO<MdMedPubfld> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }
    @Override
    public Map<String, String> findDimfldFromRedis(List<String> idList) {
        List<String> newIdList = new ArrayList<>();
        for(String id:idList) {
            newIdList.add(CommonConstant.REDIS_PREF_PUBFLD+id);
        }
        List<String> redisList = null;
        Map<String,String> returnData = new HashMap<>();
        List<String> existList = new ArrayList<>();
        List<String> notExistList = new ArrayList<>();
        String idPubfld,cdTbDim,sdDimtp;
        if(redisList!=null) {
            JSONObject jsonObject;
            for(String str:redisList) {
                if(StringUtils.isNotBlank(str)) {
                    jsonObject = JSONObject.parseObject(str);
                    idPubfld = jsonObject.getString("idPubfld");
                    cdTbDim = jsonObject.getString("cdTbDim");
                    sdDimtp = jsonObject.getString("sdDimtp");
                    isDimneedCheck(returnData,idPubfld,cdTbDim,sdDimtp);
                    existList.add(idPubfld);
                }
            }
        }
       for(String id:idList) {
           if(!existList.contains(id)) {
               notExistList.add(id);
           }
       }
       if(notExistList.size()>0) {
           List<MdMedPubfld> pubflds = this.findByIdList(notExistList);
           Map<String,Object> dataMap = new HashMap<>();
           for(MdMedPubfld fld:pubflds) {
               idPubfld = fld.getIdPubfld();
               if(idPubfld.startsWith("id_dim_")) {
                   cdTbDim = fld.getCdTbDim();sdDimtp = fld.getSdDimtp();
                   dataMap = new HashMap<>();
                   dataMap.put("idPubfld",idPubfld);
                   dataMap.put("cdTbDim",cdTbDim);
                   dataMap.put("sdDimtp",sdDimtp);
                   isDimneedCheck(returnData,idPubfld,cdTbDim,sdDimtp);
               }
           }
       }
        return returnData;
    }

    void isDimneedCheck(Map<String,String> returnData,String idPubfld,String cdTbDim,String sdDimtp) {
        if("2".equals(sdDimtp) && StringUtils.isNotBlank(cdTbDim)) {
            returnData.put(idPubfld.toUpperCase(),cdTbDim);
        }
    }
}
